package com.xuzimian.globaldemo.office.excel.pojo;

import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/***
 * @author xuzimian
 */
public class ExcelTemplate {

    private ExcelTemplate() {
    }

    public ExcelTemplate(String sheetName) {
        this.sheetName = sheetName;
    }

    private ExcelReplaceAddress excelReplaceAddress = new ExcelReplaceAddress();

    private String sheetName;

    private Workbook sourceWB;

    private Workbook outputWB;

    private Integer sourceSheetLastRowNum;

    public Integer getSourceSheetLastRowNum() {
        return sourceSheetLastRowNum;
    }

    public void setSourceSheetLastRowNum(Integer sourceSheetLastRowNum) {
        this.sourceSheetLastRowNum = sourceSheetLastRowNum;
    }

    public String getSheetName() {
        return this.sheetName;
    }

    public Sheet getSourceSheet() {
        return this.sourceWB.getSheet(sheetName);
    }

    public Sheet getOutputSheet() {
        return this.outputWB.getSheet(sheetName);
    }

    public Workbook getSourceWB() {
        return this.sourceWB;
    }

    public void setSourceWB(Workbook sourceWB) {
        this.sourceWB = sourceWB;
    }

    public Workbook getOutputWB() {
        return this.outputWB;
    }

    public void setOutputWB(Workbook outputWB) {
        this.outputWB = outputWB;
    }

    public ExcelTemplate sourceWB(Workbook sourceWB) {
        this.sourceWB = sourceWB;
        return this;
    }

    public ExcelTemplate outputWB(Workbook outputWB) {
        this.outputWB = outputWB;
        return this;
    }

    public ExcelReplaceAddress getExcelReplaceAddress() {
        return this.excelReplaceAddress;
    }

    public ExcelAddress getExcelAddress(String firstKey, String secondKey) {
        return excelReplaceAddress.getAddress(firstKey, secondKey);
    }

    public Map<String,ExcelAddress> getExcelAddress(String firstKey) {
        return excelReplaceAddress.getAddress(firstKey);
    }

    public void getReplaceCell(Cell newCell) {
        CellType cellType = newCell.getCellType();
        Optional<String> cellValue = Optional.empty();
        switch (cellType) {
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(newCell)) {
                cellValue = Optional.ofNullable(newCell.getDateCellValue()).map(d -> d.toGMTString());
            } else {
                cellValue = Optional.ofNullable(newCell.getNumericCellValue()).map(d -> String.valueOf(d));
            }
            break;
        case STRING:
            cellValue = Optional.ofNullable(newCell.getRichStringCellValue()).map(d -> d.getString());
            break;
        case BOOLEAN:
            cellValue = Optional.ofNullable(newCell.getBooleanCellValue()).map(d -> String.valueOf(d));
            break;
        case FORMULA:
            cellValue = Optional.ofNullable(newCell.getBooleanCellValue()).map(d -> d.toString());
            break;
        default:
            break;
        }

        if (cellValue.isPresent()) {
            Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
            Matcher matcher = pattern.matcher(cellValue.get());

            if (matcher.find()) {
                this.excelReplaceAddress.saveAddress(newCell.getColumnIndex(), newCell.getRowIndex(), matcher.group(1));
            }
        }
    }

}